<template>
	<view class="login-entry">
		<!-- navbar -->
		<g-navbar title="登录" />
		<!-- logo -->
		<view class="logo-wrapper">
			<image
				src="https://mp-a6359cef-e1f5-4478-96f8-00fab19c712c.cdn.bspapp.com/cloudstorage/2d7b1f38-72fa-4882-8509-45835e81f3df.png"
				class="logo" show-menu-by-longpress />
		</view>
		<!-- tips -->
		<view class="tip-title">
			请登录后继续操作
		</view>
		<!-- login btn -->
		<view class="login-btn-wrapper">
			<g-button btn_text="微信授权登录" :width="686" :height="104" :fontSize="32"
				@click="global.authorization('/pages_login/login_entry/loginEntry')" />
			<view class="vcode-login-btn" @click="jump(jumpType.VCODE_LOGIN)">
				验证码登录</view>
		</view>
		<!-- other enrty login -->
		<view class="other-login-entry">
			<view class="entry-item" @click="jump(jumpType.PASSWORD_LOGIN)">
				密码登录
			</view>
			<view class="entry-item" @click="jump(jumpType.REGISTER)">
				账号注册
			</view>
			<view class="entry-item" @click="jump(jumpType.RESET_PASSWORD)">
				忘记密码
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { onLoad } from '@dcloudio/uni-app';
	import { Ref, ref } from 'vue';
	import REST_URL from "@/http/http.restUrl";
	import { useUserData } from '@/hooks/useUserData';
	import { useToken } from '@/hooks/useToken';
	import global from '@/global';
	import { VERSION_TYPE } from '@/enum/version';
	import { useStore } from 'vuex';
	import {ROUTE_TYPE} from '@/enum/router';

	const wx_code : Ref<string> = ref('');

	const store = useStore();

	onLoad((options) => {
		// #ifdef H5
		const searchParams = new URL(window.location.href).searchParams;
		const params = Object.fromEntries(searchParams);
		console.log(params)
		wx_code.value = params.code;
		if (wx_code.value) {
			wxLoginH5();
		}
		// #endif
	})



	enum jumpType {
		VCODE_LOGIN = 'vcode',
		PASSWORD_LOGIN = 'password',
		REGISTER = 'register',
		RESET_PASSWORD = 'reset',
	}

	const jump = (type : jumpType) : void => {
		switch (type) {
			case jumpType.VCODE_LOGIN:
				uni.router.route('/pages_login/login/login?type=vcode');
				break;
			case jumpType.PASSWORD_LOGIN:
				uni.router.route('/pages_login/login/login?type=password');
				break;
			case jumpType.REGISTER:
				uni.router.route('/pages_login/register/register');
				break;
			case jumpType.RESET_PASSWORD:
				uni.router.route('/pages_login/reset_password/resetPassword');
				break;
		}
	}



	const { setUserLocalLabel, getUserInfo } = useUserData();
	const { setTokenSync } = useToken();

	interface IOptions {
		code : string
	}

	const wxLoginH5 = async () : Promise<void> => {
		console.log('微信登录，H5', wx_code.value);
		const options : IOptions = {
			code: wx_code.value,
		}

		try {
			const { data } = await uni.http.POST(REST_URL.loginByWXCode, options)

			console.log('登录成功===>', data);
			//存储token
			setTokenSync(data.access_token);
			//获取用户信息
			const userInfo = await getUserInfo(data.customer_id);
			//设置本地标签
			setUserLocalLabel(data.customer_id);
			//存储用户信息
			uni.store.local('userInfo', userInfo);
			//设置登录状态
			uni.loginManger.$resolve();
			//跳转
			// uni.router.route('/pages/home/home');
			if (store.state.version == VERSION_TYPE.EXP_VERSION) {
				uni.router.route({
					url: '/pages/home/home',
					type:ROUTE_TYPE.SWITCHTAB,
					success: () => {
						 global.resetWebUrl('/pages/home/home');
					}
				});
			} else {
				uni.router.route({
					url: '/pages/statistics/statistics',
					type:ROUTE_TYPE.SWITCHTAB,
					success: () => {
						 global.resetWebUrl('/pages/statistics/statistics');
					}
				});
			}
		} catch (e) {
			console.log('登录失败=======>', e);
			global.resetWebUrl('/pages_login/login_entry/loginEntry');
			if (e.status) {
				if(e.status == -1){
					uni.router.route('/pages_login/register/register')
				}else{
					global.followStatushandle((e.status as number));
				}
			}
		}
	}
	console.log(window.location.origin)
	console.log(encodeURIComponent(`${store.state.website}`));

	const test = () => {
	uni.router.route({
		url: '/pages/statistics/statistics',
		type:ROUTE_TYPE.SWITCHTAB,
		success: () => {
			// global.resetWebUrl('/pages/statistics/statistics');
		}
	});
	}
</script>

<style scoped lang="scss">
	@import "./loginEntry.scss";
</style>